package org.example.DesignPatterns.Mediator;

/**
 * @description:
 * @author: farprt
 * @create: 2022-09-24 09:27
 **/
public class PublicChatRoom extends ChatRoom {

    public PublicChatRoom(String name){
        super(name);
    }

    @Override
    protected void register(User user) {
        super.register(user);
        System.out.print("系统消息：欢迎【"+user.getName()+"】");
        System.out.println("】加入公共聊天室【"+name+"】,当前人数："+users.size());
    }

    @Override
    protected void deregister(User user) {
        super.deregister(user);
        System.out.println("系统消息："+user.getName());
        System.out.println("离开公共聊天室，当前人数："+users.size());
    }

    @Override
    protected void sendMsg(User from, User to, String msg) {
        //如果接收者为空，将消息发给所有人
        if(null==to){
            users.stream().forEach(user -> user.listen(from,null,msg));
            return;
        }
        //否则发给特定的人
        users.stream().filter(
                user -> user.equals(to) || user.equals(from)
        ).forEach(
                user -> user.listen(from, to, msg)
        );
    }

    @Override
    protected String processMsg(User from, User to, String msg) {
        String toName="所有人";
        if(null!=to){
            toName=to.getName();
        }
        return from.getName()+"对"+toName+"说："+msg;
    }
}